<?php

global $_W, $_GPC;
include 'function.php';

$ops = array('list', 'update', 'delete');
$op  = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'list';

if ($op == 'list') {
    $teacherId = safe_gpc_string($_GPC['teacher']);
    $query     = load()->object('query');
    $teachers  = $query->from('dgjy_teacher')->where('teacher_pid', 0)->getall();//动态列表的分类

    $sql   = "SELECT * FROM " . tablename('dgjy_appoint');
    $where = " WHERE uniacid=:uniacid ";

    //教师查找
    if (!empty($teacherId)) {
        $where = $where . " AND teacher_id = " . $teacherId;
    }

    $pageIndex  = max(1, intval($_GPC['page']));
    $pageSize   = 10;
    $total      = pdo_fetchcolumn("SELECT count(*) FROM" . tablename('dgjy_appoint') . $where, array(':uniacid' => $_W['uniacid']));
    $pagination = pagination($total, $pageIndex, $pageSize);
    $appoint    = pdo_fetchall($sql . $where . " ORDER BY createtime DESC LIMIT " . ($pageIndex - 1) * $pageSize . "," . $pageSize, array(':uniacid' => $_W['uniacid']));

    include $this->template('appoint/appoint');
}

if ($op == 'delete') {
    $id = intval($_GPC['appoint_id']);
    if (!empty($id)) {
        $query   = load()->object('query');
        $teacher = $query->from('dgjy_appoint')->where('uniacid', $_W['uniacid'])->where('id', $id)->get();//根据id查找该老师
        if (!empty($teacher)) {
            if ($teacher['uniacid'] == $_W['uniacid']) {
                $res = pdo_delete('dgjy_appoint', array('uniacid' => $_W['uniacid'], 'id' => $id));//删除老师
                if (!empty($res)) {
                    itoast('恭喜！删除成功！', referer(), 'success');
                } else {
                    itoast('抱歉！删除失败！', referer(), 'error');
                }

            } else {
                itoast('抱歉！您没有权限删除此信息！', $this->createWebUrl('teacher'), 'error');
            }
        } else {
            message('抱歉！您要删除的信息不存在！', $this->createWebUrl('teacher'), 'error');
        }
    } else {
        message('警告！请勿提交非法网址！', $this->createWebUrl('teacher'), 'error');
    }
}
?>